package com.hexinfo.dmpro.component.comparison.entity;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.hexinfo.dmpro.component.comparison.enums.TableContrastEnums;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * @Package com.hexinfo.dmpro.component.comparison.entity
 * @ClassName TableContrastResultEntity
 * @Description 用于表示比对任务的基本情况和运行状态
 * @Author Huang Yanyihe
 * @Date 2023/9/10 21:30
 * @Version v1.0
 **/
@Data
@NoArgsConstructor
public class TableContrastResultEntity {

    //源表数据总量
    private Long tableDataTotal = 0L;

    //目标表总量
    private Long targetTableDataTotal = 0L;

    //源数据读取量
    private Long sourceContrastNum = 0L;

    // 对比类型(热备数据对比,冷备数据对比)
    private String contrastType;

    // 比对任务唯一标识
    private String tableFlag;

    //源库名
    private String sourceDbName;

    //源表名
    private String sourceTableName;

    //目标库名
    private String targetDbName;

    //目标表名
    private String targetTableName;

    //比对任务状态 RUNNING,FAIL,ACHIEVE
    private String status = TableContrastEnums.RUNNING.getValue();

    //错误信息
    private String falseMessage = "";

    //比对任务开始时间
    private String contrastStartTime= DateUtil.now();

    //表级比对开始时间
    private String tableContrastStartTime="NotStart";

    //表级比对结束时间
    private String tableContrastEndTime="NotEnd";

    //明细比对开始时间
    private String detailsContrastStartTime="NotStart";

    //明细比对结束时间
    private String detailsContrastEndTime="NotEnd";

    /**
     * @Method TableContrastResultEntity
     * @Param tableContrastParam
     * @Return TableContrastResultEntity
     * @Description 输入实体到结果实体的构造器
     * @Author Huang Yanyihe
     * @Date 2023/9/18 17:27
     * @Version V1.0
     */
    public TableContrastResultEntity(TableContrastParamEntity tableContrastParam){
        this.contrastType = tableContrastParam.getContrastType();
        this.tableFlag = tableContrastParam.getTableFlag();
        this.sourceTableName =tableContrastParam.getSourceTableName();
        this.sourceDbName = tableContrastParam.getSourceDbName();
        this.targetTableName = tableContrastParam.getTargetTableName();
        this.targetDbName = tableContrastParam.getTargetDbName();
    }

    /**
     * @Method setFailStatus
     * @Return void
     * @Description 设置比对任务结果为失败
     * @Author Huang Yanyihe
     * @Date 2023/9/18 17:28
     * @Version V1.0
     */
    public void setFailStatus(){
        status = TableContrastEnums.FAIL.getValue();
    }

    /**
     * @Method setAchieveStatus
     * @Return void
     * @Description 设置表比对任务结果为成功
     * @Author Huang Yanyihe
     * @Date 2023/9/18 17:28
     * @Version V1.0
     */
    public void setAchieveStatus(){
        status = TableContrastEnums.ACHIEVE.getValue();
    }

    /**
     * @Method getContrastTaskPercent
     * @Return java.lang.String
     * @Description 获取明细比对任务进行进度
     * @Author Huang Yanyihe
     * @Date 2023/9/20 13:37
     * @Version V1.0
     */
    public String getContrastTaskPercent(){
        if(tableDataTotal.longValue() == 0l){
            return "detailsContrastTask is not start!";
        }
        else{
            double num = NumberUtil.div(sourceContrastNum.toString(), tableDataTotal.toString(),4)
                    .multiply(BigDecimal.valueOf(100)).intValue();
            return Double.toString(num)+"%";
        }
    }
}
